package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishFlavorMapper;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


@Service
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;


    @Autowired
    private DishFlavorMapper dishFlavorMapper;

    @Autowired
    private SetmealDishMapper setmealDishMapper;

    //--1--新增菜品[包含口味]
    @Override
    public void saveOne(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);  //只剩四个公共字段，不用处理

        dishMapper.insertOne(dish); //设置主键返回
        Long addDishid = dish.getId();

        //--获取口味列表集合
        List<DishFlavor> flavors=dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0){
            flavors.forEach(f->{
                f.setDishId(addDishid);
            });
        }
        dishFlavorMapper.insertBatch(flavors);

    }







    //-2--分页查询
    @Override
    public PageResult selectPage(DishPageQueryDTO dishPageQueryDTO) {
        PageHelper.startPage(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize());
        Page<DishVO> dishVOPage=dishMapper.selectPage(dishPageQueryDTO);
        return new PageResult(dishVOPage.getTotal(),dishVOPage.getResult());
    }

    //--3--批量删除
    @Override
    @Transactional
    public void deleteByIds(List<Long> ids) {

        //--查询当前需要删除菜品的状态：0 停售 1 起售
        Integer count=dishMapper.countByStatusAndIds(ids);
        if(count!=null &&count>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
        }

        //--查询当前需要删除菜品：是否关联套餐
        count=setmealDishMapper.countBySetmealDishAndIds(ids);
        if(count!=null &&count>0){
            throw new DeletionNotAllowedException(MessageConstant.DISH_BE_RELATED_BY_SETMEAL);
        }

        dishMapper.deleteByIds(ids);       //删除菜品
        dishFlavorMapper.deleteByIds(ids); //删除口味
    }

    //--4--根据id查菜品（包含口味）
    @Override
    public DishDTO getDishById(Long id) {
        Dish dish=dishMapper.getDishById(id);
        List<DishFlavor> flavors = dishFlavorMapper.getByDishId(id);

        DishDTO dishDTO=new DishDTO();
        BeanUtils.copyProperties(dish,dishDTO);
        dishDTO.setFlavors(flavors);
        return dishDTO;
    }

    //--3--修改菜品
    @Override
    @Transactional
    public void updateDish(DishDTO dishDTO) {
        Dish dish=new Dish();
        BeanUtils.copyProperties(dishDTO,dish);
        dishMapper.updateDish(dish);

        //--获取口味列表集合
        dishFlavorMapper.deleteByDishId(dish.getId()); //清空菜品口味
        List<DishFlavor> flavors=dishDTO.getFlavors();
        if(flavors!=null && flavors.size()>0){
            flavors.forEach(f->{
                f.setDishId(dish.getId());
            });
        }
        dishFlavorMapper.insertBatch(flavors); //直接添加
    }



    @Override
    public void updateDishStatus(Integer status,Long id) {
        dishMapper.updateDishStatus(status,id);
    }


    //根据分类id查询菜品
    @Override
    public List<DishVO> selectDishByCategoryId(Long categoryId) {
        List<Dish> dishList=dishMapper.selectDishByCategoryId(categoryId);

        List<DishVO> dishVOList=dishList.stream().map(item->{
            DishVO dishVO=new DishVO();
            BeanUtils.copyProperties(item,dishVO);

            List<DishFlavor> dishFlavors = dishFlavorMapper.getByDishId(dishVO.getId());
            dishVO.setFlavors(dishFlavors);
            return dishVO;
        }) .collect(Collectors.toList());
        return dishVOList;
    }
}
